<template>
  <div class="body">
    <div class="textbox">
      <textarea
        maxlength="70"
        id="content"
        v-model="content"
        placeholder="請輸入短信内容，不要超過70個字哦"
      ></textarea>
      <div class="weui_textarea_counter">
        <span id="num">0</span>/70
      </div>
      <div class="but" @click="submit()">發送短信</div>
    </div>
  </div>
</template>

<script>
import { isLogin } from '@/api/login'
import { sendSms } from '@/api/society'

export default {
  name: 'SendSms',
  data () {
    return {
      content: '',
      formData: {}
    }
  },
  watch: {
    content (val) {
      var conten = document.getElementById('num')
      if (conten && val) {
        val = val.replace(/\n|\r/gi, '')
        conten.innerText = val.length
      }
    }
  },
  created () {
    this.isLogin()
  },
  methods: {
    isLogin () {
      isLogin().then(res => {
        if (res.code === 0) {
          alert('请先登录')
          this.$router.push('/login')
        }
      })
    },
    submit () {
      var param = (window.location.hash).split('#')[1]
      var aid = this.$route.query.aid
      var sid = this.$route.query.sid
      var target = localStorage.getItem('target') // 群发人员
      if (param === 'person_sms') {
        this.formData = {
          sid: sid,
          target: target,
          content: this.content
        }
      } else {
        this.formData = {
          sid: sid,
          aid: aid,
          content: this.content
        }
      }
      console.log(this.formData)
      sendSms(this.formData).then(res => {
        if (res.code === 1) {
          alert('发送成功')
          this.$router.push('/member_center')
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style type="text/css" scoped>
* {
  margin: 0;
  padding: 0;
}

.body {
  background: rgba(247, 202, 201, 0.42);
  font-family: PingFangSC;
  min-height: 800px;
}

.textbox {
  width: 95%;
  /* margin: 30px auto auto auto; */
  margin: 0 auto;
  padding-top: 30px;
}

textarea {
  border-radius: 10px;
  width: 100%;
  height: 150px;
  line-height: 23px;
  outline: none;
  border: none;
  padding-top: 5px;
  font-size: 16px;
  padding-left: 15px;
  padding-right: 15px;
}

.but {
  color: white;
  height: 44px;
  background: rgb(240, 118, 110);
  border-radius: 34px 34px 34px 34px;
  margin: 40px auto auto auto;
  border: solid 1px rgb(240, 118, 110);
  width: 80%;
  text-align: center;
  line-height: 44px;
  font-size: 22px;
}

.weui_textarea_counter {
  float: right;
  line-height: 34px;
}
</style>
